<!DOCTYPE html>
<html>
	<head>
		<meta charset="UTF-8">
		<meta name="viewport" content="width=640px,user-scalable=no,target-densitydpi=device-dpi" />
		<link rel="stylesheet" href="../css/css.css" type="text/css" />
		<script type="text/javascript" src='../js/jquery.min.js'></script>
		<script type="text/javascript" src='../js/main_in.js'></script>
		<script type="text/javascript" src='../js/common.js'></script>
		<title></title>
		<script type="text/javascript">
			var out_trade_no = getUrlParam('out_trade_no');
			var uid = window.localStorage["uid"];
			var loginname = window.localStorage["loginname"];
			var ServiceCharge = 0;
			var Tax = 0;
			var Commission = 0;
			var Profit = 0;
			alipay_bgid(1);
			$(function(){
				fade_out_pull('fade_out_loading');
				if(out_trade_no != '' || out_trade_no != null || out_trade_no != undefined){
//					alert("运单生成中，请勿关闭页面！");
					getbody();
				}
			});
			
			function getbody(){
				$.ajax({
                        url: "http://api.unair.cn/api/AliPayBL?apID="+out_trade_no,
                        type: "GET",
//                        data: body,
                        success: function(data) {
                                var d = new Date();
                                var str = '';
                                 str += d.getFullYear()+'-';
                                 str  += d.getMonth() + 1+'-';
                                 str  += d.getDate()+'';
                                 str += ' ';
                                 str += d.getHours()+':';
                                 str  += d.getMinutes()+':';
                                str+= d.getSeconds()+'';
                                
                                var str2 = data.APID;
                                var CustomerName = data.Consignor;
                                var cus_CityID = data.FromAreaID;
                                var cus_CityName = data.FromArea;
                                var cus_Address = data.FromAddress;
                                var cus_Contact = data.Contact1;
                                var cus_ContactTel = data.ContactTel1;
                                var addressname = data.Consignee;
                                var add_CityID = data.ToAreaID;
                                var add_CityName = data.ToArea;
                                var add_Address = data.ToAddress;
                                var add_Contact = data.Contact2;
                                var add_ContactTel = data.ContactTel2;
                                var FreightName = data.ItemName;
                                var Number = data.ItemCount;
                                var Freight = data.Freight;
                                var Grade = data.ItemPrice;
                                var insurance_money_value1 = data.Insurance1;
                                var insurance_money_value = data.Insurance2;
                                var MinCharge1 = data.MinCharge1;
                                var MinCharge2 = data.MinCharge2;
                                var RobberyTheft = data.RobberyTheft;
                                var DeductAmount = data.DeductAmount;
                                var DeductRate = data.DeductRate;
                                var Ratio1 = data.Ratio1;
                                var Ratio2 = data.Ratio2;
                                var Rid = data.RidOfInsuranceMain;
                                var PlateNo = data.PlateNo;
                                var SNID = data.CHEGUAHAO;
                                var SNScheFlightNo = data.DriverName;
                                var SNCorpName = data.DriverMobile;
                                
            var formal = {
                '' : '',
                '5' : 0.03,
                '12' : 0.07,
                '30' : 0.17,
                '60' : 0.33,
                '0.01' : 0,
                '50' : 0.28,
                '70' : 0.49,
                '75' : 0.53,
                '80' : 0.56,
                '85' : 0.6,
                '90' : 0.63,
                '95' : 0.67,
                '100' : 0.7,
                '105' : 0.74,
                '110' : 0.77,
                '115' : 0.81,
                '120' : 0.84,
                '125' : 0.88,
                '130' : 0.91,
                '140' : 0.98,
                '155' : 1.09,
                '165' : 1.16
            };
            
			ServiceCharge = formal[insurance_money_value];
			Tax = (insurance_money_value - insurance_money_value1)/1.06*0.06*1.12;
			Commission = 50;
			Profit = insurance_money_value - insurance_money_value1 -50 - Tax - ServiceCharge;
                                
                                var body = {"Token":"22e8f734-dae9-494e-a743-4c4a64af4ce2",
                                                    "Users":{"id":uid,"username":loginname,"cpid":"11583-47254","cpname":"\u5361\u822a\u7f51\u7edc","ndid":"63599-70527","ndname":"\u5361\u822a\u7f51\u7edc","cmid":""},
                                                    "MasterBargain":{"CPID":"11583-47254","Barcode":PlateNo,"NDIDStart":"63599-70527","NDNameStart":"\u5361\u822a\u7f51\u7edc",
                                                                        "NDIDEnd":"","NDNameEnd":"","CPIDStart":"11583-47254","CPNameStart":"\u5361\u822a\u7f51\u7edc","CPIDEnd":"","CPNameEnd":"",
                                                                        "PCStart":cus_CityID,"PCNameStart":cus_CityName,  //始发城市
                                                                        "PCEnd":add_CityID,"PCNameEnd":add_CityName,                  //到达城市
                                                                        "STree":"","ReceiveDate":str,                                     //时间
                                                                        "CustomerInquiryId":SNID,"RequestDate":"","ProjectName":"","CMID":3930,
                                                                        "CMName":CustomerName,"CMAddr":cus_Address,"CMLong":'',"CMLat":'',    //始发客户、地址
                                                                        "CMContact":cus_Contact,"CMTel":cus_ContactTel,                                                   //始发人，电话
                                                                        "SDID":"","SDName":"","RCID":0,
                                                                        "RCName":addressname,"RCAddr":add_Address,"RCLong":'',"RCLat":'',       //到达客户、地址
                                                                        "RCContact":add_Contact,"RCTel":add_ContactTel,                                                     //到达人，电话
                                                                        "FTType":"0",
                                                                        "FTName":FreightName,                                                                      //货物名
                                                                        "FTPackage":"纸箱","TransportMode":"0","DeliveryMode":"0","SettleMode":"1","RELocation":"\u5361\u822a\u7f51\u7edc","REState":10,"PricingWeight":0,"InsuredValue":0,"IsMessage":"0","IsReturn":"0","REType":"0","RECount":"","REID":"","OtherRemark":""},
                                                    "Bargain":{"SGNDID":"63599-70527","SGNDName":"\u5361\u822a\u7f51\u7edc","CPID":"11583-47254","NDID":"63599-70527","CreateType":2,"CPIDLast":3930,
                                                                        "CPNameLast":CustomerName,"CPAddrLast":cus_Address,"LastLong":'',"LastLat":'',
                                                                        "CPContactLast":cus_Contact,"CPTelLast":cus_ContactTel,
                                                                        "NDIDLast":"63599-70527","NDNameLast":"\u5361\u822a\u7f51\u7edc",
                                                                        "BGSDAddr":cus_Address,"SDLong":'',"SDLat":'',
                                                                        "SDCityID":cus_CityID,"SDCityName":cus_CityName,
                                                                        "IsTakeDelivery":0,"Barcode":PlateNo,
                                                                        "ReceiveDate":str,
                                                                        "InquiryId":SNID,"TransportMode":"0","ServiceMode":"0","SettleMode":"1","ApprovalState":0,
                                                                        "Count":Number,"RemainCount":Number,
                                                                        "Weight":0,"Volume":0,"PricingWeight":0,"PricingMode":"2","Price":0,
                                                                        "Freight":Freight,                                                                   //运费
                                                                        "TakeCharge":"","GiveCharge":"","AddCharge":"","OtherCharge":"","OverallIncome":Freight,"CreateBy":uid,"CreateName":loginname,"IsReturn":"0","Remark":"","SNID":"","SNCorpID":"","SNCorpName":SNCorpName,"SNScheFlightNo":SNScheFlightNo,"SNScheDepartTime":"","SNArriveTime":"","CashPayAmount":0,"ArrivalPayAmount":0,"DebtPayAmount":0,
                                                                        "CityNameLast":cus_CityName,"CityIDLast":cus_CityID,
                                                                        "IsInsure":"1","AgentName":"","twoagent":null,"Sales":"","SalesName":'',
                                                                    },
                                                                    "Freight":[{"Accoc":"1","FreightName":FreightName,"Package":"纸箱","Number":Number,"Weight":"","Volume":"","PricingWeight":"","Insure":"","Remark":""}],
                                    }
                                    					var req = {
                                                            "cargoFreight":Freight,
                                                            "claimLimit":Grade,
                                                            "claimLimit2":RobberyTheft,
                                                            "cargeValue":"",
                                                            "mpAmount":DeductAmount,
                                                            "mpRate":DeductRate,
                                                            "policyRate":Ratio2,
                                                            "insuranceFee":insurance_money_value,
                                                            "RidOfInsuranceMain":Rid,
                                                            "newFromAID":cus_CityID,
                                                            "newFromAddress":cus_CityName,
                                                            "newToAID":add_CityID,
                                                            "newToAddress":add_CityName,
                                              			}
									
                                    bargain_add(body,out_trade_no,req);
                                
                        },
                        error: function(data) {
                            alert('提交运单失败')
                        }
                    });
			}
			
function bargain_add(body,out_trade_no,req){
//    alert(body['Req']['insuranceFee']);
//    alert(body['Req']['mpRate']);
//    alert(body['Req']['policyRate']);
var BGID = '';
//alert(ServiceCharge);return false;
    
                            $.ajax({
                                    url: "http://api.unair.cn/api/LongKunRequestLogCL",
                                    type: "POST",
                                    data: body,
                                    success: function (data) {
                                        BGID = data['bargain']['BGID'];
                                        if(BGID != ''){
												alipay_bgid(2,BGID);
                                                do_insure(out_trade_no,BGID,req)
                                        }else{
                                        	alipay_bgid(22,BGID);   //bgid无返回
                                        }
                                    },
                                    error: function (data) {
                                    	alipay_bgid(21,BGID);      //api调用失败
                                        alert("开单失败！请重试或联系客服");
                                    }
                                });
                                
}

function do_insure(out_trade_no,BGID,req){
    req['channelOrderId'] = BGID;
//    alert(JSON.stringify(req));return false;
    
                                $.ajax({
                                    url: "http://api.unair.cn/api/LongKunRequestBL?type=1",
                                    type: "POST",
                                    data: req,
                                    success: function (data) {
                                        alipay_bgid(3,BGID);
                                    },
                                    error: function (data) {
                                    	alipay_bgid(31,BGID);		//投保失败
                                        alert("开单成功，投保失败！请联系客服")
                                    }
                                });
}

function alipay_bgid(state_v,BGID){
			
                            $.ajax({
                                    url: "http://api.unair.cn/api/AliPayBL?APID=" + out_trade_no,
                                    type: "POST",
                                    data: {
                                        APID: out_trade_no,
                                        BGID: BGID,
                                        State:state_v,
                                        ServiceCharge:ServiceCharge,
                                        Tax:Tax,
                                        Commission:Commission,
                                        Profit:Profit
                                    },
                                    success: function (data) {
                                    	if(state_v == 3){
                                    		clicked("bgsearch.html");
                                    	}
//                                      clicked("bgsearch.html");
                                    },
                                    error: function (data) {
                                        
                                    }
                                });
                                
}
		</script>
	</head>
	<body>
<div style="display: block" class="outside_pull"></div>
<img style="display: block" src="../img/loading.gif" class="fade_out_img" id="fade_out_loading" />
	</body>
</html>
